﻿'========================================================================
'功能:郵件參數資料庫存取物件
'作者:andy
'日期: 2011/3/14
'========================================================================

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports System.Text
Imports System.Data
Imports System.Net.Mail
Namespace AssetManage.BBL
    Public Class MailBBL



        ''' <summary>
        ''' 寄送合計型的mail給user
        ''' </summary>
        ''' <param name="drs"></param>
        ''' <param name="webPath"></param>
        ''' <param name="MailFrom"></param>
        ''' <param name="SMTP"></param>
        ''' <param name="MailTo"></param>
        ''' <param name="MailSubject"></param>
        ''' <param name="MailBody"></param>
        ''' <param name="dbName"></param>
        ''' <param name="ReplyDeadLine"></param>
        ''' <remarks></remarks>
        Public Sub SendMailDataSum(ByVal drs() As DataRow, ByVal webPath As String, ByVal MailFrom As String, ByVal SMTP As String, ByVal MailTo As String, ByVal MailSubject As String, ByVal MailBody As String, ByVal dbName As String, ByVal ReplyDeadLine As String, ByVal webPathQuery As String, ByVal Language As String)
            Dim body As String
            Dim emp_id As String = ""
            Select Case Language
                Case "en-us"
                    body = "<table border='1'> "
                    body += "<tr>"
                    body += "<td>Category "
                    body += "</td>"
                    body += "<td>Qty"
                    body += "</td>"
                    body += "</tr>"


                    For Each dr As DataRow In drs
                        emp_id = dr("keeping_person").ToString()
                        body += "<tr>"
                        body += "<td>" + dr("name").ToString()
                        body += "</td>"
                        body += "<td><a href='" + webPathQuery + "?AssetType=" + dr("asset_type").ToString + "&UID=" + dr("keeping_person").ToString() + "'><font color='Red'>___" + dr("Qty").ToString() + "___</font></a>"
                        body += "<td><a href='" + webPath + "?Type=Reply&AssetType=" + dr("asset_type").ToString + "&UID=" + dr("keeping_person").ToString() + "'>&nbsp;Reply&nbsp;</a>"
                        body += "</font></td>"
                        body += "</tr>"
                    Next
                    body += "</table>"


                    body += "<br /><br /><a href='" + webPath + "?Type=Reply&UID=" + drs(0)("keeping_person").ToString() + "'>" + "Reply all " + "</a>"
                    body += "<br /><br /><a href='" + webPathQuery + "?UID=" + drs(0)("keeping_person").ToString() + "'>" + "Query All " + "</a>"
                    body += "<br /><br /><a href='" + webPath + "?UID=" + drs(0)("keeping_person").ToString() + "&Type=Cancel'>Cancel Reply </a><br /><br />"


                Case "zh-cn"
                    body = "<table border='1'> "
                    body += "<tr>"
                    body += "<td>类别名称"
                    body += "</td>"
                    body += "<td>数量"
                    body += "</td>"
                    body += "</tr>"


                    For Each dr As DataRow In drs
                        body += "<tr>"
                        body += "<td>" + dr("name").ToString()
                        body += "</td>"
                        body += "<td><a href='" + webPathQuery + "?AssetType=" + dr("asset_type").ToString + "&UID=" + dr("keeping_person").ToString() + "'><font color='Red'>___" + dr("Qty").ToString() + "___</font></a>"
                        body += "<td><a href='" + webPath + "?Type=Reply&AssetType=" + dr("asset_type").ToString + "&UID=" + dr("keeping_person").ToString() + "'>&nbsp;回报&nbsp;</a>"
                        body += "</font></td>"
                        body += "</tr>"
                    Next
                    body += "</table>"


                    body += "<br /><br /><a href='" + webPath + "?Type=Reply&UID=" + drs(0)("keeping_person").ToString() + "'>" + "回报全部" + "</a>"
                    body += "<br /><br /><a href='" + webPathQuery + "?UID=" + drs(0)("keeping_person").ToString() + "'>" + "查询全部" + "</a>"
                    body += "<br /><br /><a href='" + webPath + "?UID=" + drs(0)("keeping_person").ToString() + "&Type=Cancel'>取消回报</a><br /><br />"
                Case Else
                    body = "<table border='1'> "
                    body += "<tr>"
                    body += "<td>類別名稱"
                    body += "</td>"
                    body += "<td>數量"
                    body += "</td>"
                    body += "</tr>"


                    For Each dr As DataRow In drs
                        body += "<tr>"
                        body += "<td>" + dr("name").ToString()
                        body += "</td>"
                        body += "<td><a href='" + webPathQuery + "?AssetType=" + dr("asset_type").ToString + "&UID=" + dr("keeping_person").ToString() + "'><font color='Red'>___" + dr("Qty").ToString() + "___</font></a>"
                        body += "<td><a href='" + webPath + "?Type=Reply&AssetType=" + dr("asset_type").ToString + "&UID=" + dr("keeping_person").ToString() + "'>&nbsp;回報&nbsp;</a>"
                        body += "</font></td>"
                        body += "</tr>"
                    Next
                    body += "</table>"


                    body += "<br /><br /><a href='" + webPath + "?Type=Reply&UID=" + drs(0)("keeping_person").ToString() + "'>" + "回報全部" + "</a>"
                    body += "<br /><br /><a href='" + webPathQuery + "?UID=" + drs(0)("keeping_person").ToString() + "'>" + "查詢全部" + "</a>"
                    body += "<br /><br /><a href='" + webPath + "?UID=" + drs(0)("keeping_person").ToString() + "&Type=Cancel'>取消回報</a><br /><br />"

            End Select
           
            Dim MailType As String = System.Configuration.ConfigurationManager.AppSettings("MailType")

            Dim mail As New MailMessage()
            Dim uBBl As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
            mail.Subject = MailSubject
            If MailBody.IndexOf("|$|資料|$|") = -1 Then
                mail.Body = MailBody + body
            Else
                mail.Body = MailBody.Replace("|$|資料|$|", body)
            End If


            ''如果有代理人則加入cc
            If MailType.ToLower = "online" Then '正式才要cc
                Dim agentID As String = uBBl.GetVIPAgent(emp_id, dbName).Trim
                If agentID <> "" Then
                    Try
                        mail.CC.Add(New MailAddress(uBBl.GetEmpData(agentID, dbName).Rows(0)("EMail").ToString()))
                        mail.Subject = mail.Subject + ",這是代理人信件,請不要任意刪除!"
                    Catch ex As Exception

                    End Try
                Else ''不是VIP才給MailTo
                    Try
                        mail.To.Add(New MailAddress(MailTo))
                    Catch ex As Exception

                    End Try

                End If
            Else ''測試時給特定人員
                mail.To.Add(New MailAddress(MailTo))

            End If


            mail.From = New System.Net.Mail.MailAddress(MailFrom, "OA系統通知")

            mail.IsBodyHtml = True
            Dim smtpServer As SmtpClient = New SmtpClient(SMTP)
            Try
                smtpServer.Send(mail)
            Catch ex As Exception

            End Try









        End Sub


        Public Sub InsertSendMailParam(ByVal ParamID As String, ByVal MailSubject As String, ByVal MailBody As String, ByVal ReturnDays As String, ByVal ResendDays As String, ByVal ResendTimes As String _
                                              , ByVal UID As String, ByVal ScheduleType As String, ByVal SendDate As String, ByVal TopN As String, ByVal DeptNo1 As String, ByVal DeptNo2 As String, ByVal EquQty As String, _
                                                ByVal AssetType As String, ByVal TakeOfficeDate As String, ByVal HouseID As String, ByVal Status As String, ByVal Monthst As String, ByVal Dayst As String, ByVal MailSubjectChn As String, ByVal MailSubjectEng As String, ByVal MailBodyChn As String, ByVal MailBodyEng As String, ByVal dbName As String)

            Dim db1 As AssetManage.DAO.SenMailDAO = New AssetManage.DAO.SenMailDAO(dbName)
            db1.InsertSendMailParam(ParamID, MailSubject, MailBody, ReturnDays, ResendDays, ResendTimes, UID, ScheduleType, SendDate, TopN, DeptNo1, DeptNo2, EquQty, AssetType, TakeOfficeDate, HouseID, Status, Monthst, Dayst, MailSubjectChn, MailSubjectEng, MailBodyChn, MailBodyEng)
        End Sub

        Public Sub UpdateSendMailParam(ByVal ParamID As String, ByVal MailSubject As String, ByVal MailBody As String, ByVal ReturnDays As String, ByVal ResendDays As String, ByVal ResendTimes As String _
                                             , ByVal UID As String, ByVal ScheduleType As String, ByVal SendDate As String, ByVal TopN As String, ByVal DeptNo1 As String, ByVal DeptNo2 As String, ByVal EquQty As String, _
                                               ByVal AssetType As String, ByVal TakeOfficeDate As String, ByVal HouseID As String, ByVal Status As String, ByVal Monthst As String, ByVal Dayst As String, ByVal SerNo As String, ByVal MailSubjectChn As String, ByVal MailSubjectEng As String, ByVal MailBodyChn As String, ByVal MailBodyEng As String, ByVal dbName As String)
            Dim db1 As AssetManage.DAO.SenMailDAO = New AssetManage.DAO.SenMailDAO(dbName)
            db1.UpdateSendMailParam(ParamID, MailSubject, MailBody, ReturnDays, ResendDays, ResendTimes, UID, ScheduleType, SendDate, TopN, DeptNo1, DeptNo2, EquQty, AssetType, TakeOfficeDate, HouseID, Status, Monthst, Dayst, SerNo _
                                    , MailSubjectChn, MailSubjectEng, MailBodyChn, MailBodyEng)
        End Sub


        ''' <summary>
        ''' 查詢發送的信件
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetSendMail(ByVal ScheduleType As String, ByVal AssetType As String, ByVal DeptNo As String, ByVal dbName As String) As DataTable
            Dim db1 As AssetManage.DAO.SenMailDAO = New AssetManage.DAO.SenMailDAO(dbName)
            Return db1.GetSendMail(ScheduleType, AssetType, DeptNo)
        End Function
        ''' <summary>
        ''' 查詢發送的信件
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetSendMailbySerno(ByVal SerNo As Integer, ByVal dbName As String) As DataTable
            Dim db1 As AssetManage.DAO.SenMailDAO = New AssetManage.DAO.SenMailDAO(dbName)
            Return db1.GetSendMailbySerno(SerNo)
        End Function


        ''' <summary>
        ''' 刪除某一筆資料
        ''' </summary>       
        ''' <remarks></remarks>
        Public Sub DeleteSendMail(ByVal SerNo As Integer, ByVal dbName As String, ByVal UID As String)
            Dim db1 As AssetManage.DAO.SenMailDAO = New AssetManage.DAO.SenMailDAO(dbName)
            db1.DeleteSendMail(SerNo, UID)
        End Sub


        ''' <summary>
        ''' 修改信件發送條件狀態
        ''' </summary>
        ''' <param name="SerNo">流水編號</param>
        ''' <param name="Status">狀態</param>
        ''' <remarks></remarks>
        Public Sub UpdateSendMailStatus(ByVal SerNo As Integer, ByVal Status As String, ByVal dbName As String, ByVal UID As String)
            Dim db1 As AssetManage.DAO.SenMailDAO = New AssetManage.DAO.SenMailDAO(dbName)
            db1.UpdateSendMail(SerNo, Status, UID)
        End Sub
    End Class


    Public Class MailParamBBL



        ''' <summary>
        ''' 取得郵件參數設定代號資料
        ''' </summary>
        ''' <param name="kind">0=一般,1=全部</param>
        ''' <returns>取得郵件參數設定代號資料</returns>
        ''' <remarks>取得郵件參數設定代號資料</remarks>
        Public Function GetMailParamItem(ByVal kind As String, ByVal dbName As String) As DataTable
            Dim db1 As AssetManage.DAO.MailParamDAO = New AssetManage.DAO.MailParamDAO(dbName)
            Return db1.GetMailParamItem(kind)
        End Function
        ''' <summary>
        ''' 取得郵件參數設定資料
        ''' </summary>
        ''' <returns>取得郵件參數設定資料</returns>
        ''' <remarks>取得郵件參數設定資料</remarks>
        Public Function GetMailParam(ByVal dbName As String) As DataTable
            Dim db1 As AssetManage.DAO.MailParamDAO = New AssetManage.DAO.MailParamDAO(dbName)
            Return db1.GetMailParam()
        End Function

        ''' <summary>
        ''' 取得1筆郵件參數設定資料
        ''' </summary>
        ''' <returns>取得1筆郵件參數設定資料</returns>
        ''' <remarks>取得1筆郵件參數設定資料</remarks>
        Public Function GetMailParamByID(ByVal ParamID As String, ByVal dbName As String) As DataTable
            Dim db1 As AssetManage.DAO.MailParamDAO = New AssetManage.DAO.MailParamDAO(dbName)
            Return db1.GetMailParamByID(ParamID)
        End Function

        ''' <summary>
        ''' 新增1筆空白並回傳ID
        ''' </summary>
        ''' <returns>新增1筆空白並回傳ID</returns>
        ''' <remarks>新增1筆空白並回傳ID</remarks>

        Public Function InsertMailParam(ByVal UID As String, ByVal dbName As String) As String

            Dim db1 As AssetManage.DAO.MailParamDAO = New AssetManage.DAO.MailParamDAO(dbName)
            Return db1.InsertMailParam(UID)
        End Function

        Public Sub UpdateMailParam(ByVal ParamID As String, ByVal ParamDesc As String, ByVal ReturnDays As Integer, ByVal ResendDays As Integer, ByVal ResendTimes As Integer, ByVal MailSubject As String, ByVal MailBody As String, ByVal UID As String, ByVal MailSubjectChn As String, ByVal MailSubjectEng As String, ByVal MailBodyChn As String, ByVal MailBodyEng As String, ByVal dbName As String)
            Dim db1 As AssetManage.DAO.MailParamDAO = New AssetManage.DAO.MailParamDAO(dbName)
            db1.UpdateMailParam(ParamID, ParamDesc, ReturnDays, ResendDays, ResendTimes, MailSubject, MailBody, UID, MailSubjectChn, MailSubjectEng, MailBodyChn, MailBodyEng)
        End Sub

    End Class


End Namespace

